package com.local.indexconfig.controller;

import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.indexconfig.pojo.IndexConfigDeleteRequestParam;
import com.local.indexconfig.pojo.IndexConfig;
import com.local.indexconfig.pojo.IndexConfigListRequestParam;
import com.local.indexconfig.service.IndexConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/indexconfig")
public class IndexConfigController {

    @Autowired
    private IndexConfigService indexConfigService;

    /**
     * 获取配置商品列表，支持翻页
     * @param requestParam
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestBody IndexConfigListRequestParam requestParam) {
        Integer configType = requestParam.getConfigType();
        if (Objects.isNull(configType) || !(configType instanceof Integer)) {
            return new ResultUtil(201, "参数错误");
        }
        Integer page = Objects.nonNull(requestParam.getPage()) ? requestParam.getPage() : 1;
        Integer limit = Objects.nonNull(requestParam.getLimit()) ? requestParam.getLimit() : 10;
        String sort = Objects.nonNull(requestParam.getSort()) ? requestParam.getSort() : "config_rank";
        String order = Objects.nonNull(requestParam.getOrder()) ? requestParam.getOrder() : "asc";
        Integer isDeleted = Objects.nonNull(requestParam.getIsDeleted()) ? requestParam.getIsDeleted() : -1;

        PageResultUtil<IndexConfig> indexConfigList = indexConfigService.findIndexConfigList(configType, page, limit, sort, order, isDeleted);
        if (Objects.nonNull(indexConfigList)) {
            return new ResultUtil(200, "", indexConfigList);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }


    /**
     * 新增
     * @param indexConfig
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody IndexConfig indexConfig) {
        Integer saveResult = indexConfigService.save(indexConfig);
        if (saveResult > 0) {
            return new ResultUtil(200, "新增成功");
        } else {
            return new ResultUtil(201, "新增失败");
        }
    }


    /**
     * 修改
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody IndexConfig indexConfig) {
        Integer updateResult = indexConfigService.update(indexConfig);
        if (updateResult > 0) {
            return new ResultUtil(200, "修改成功");
        } else {
            return new ResultUtil(201, "修改失败");
        }
    }


    /**
     * 根据id删除，支持批量删除
     * @param indexConfigDeleteRequestParam
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil delete(@RequestBody IndexConfigDeleteRequestParam indexConfigDeleteRequestParam) {
        Integer[] ids = indexConfigDeleteRequestParam.getIds();
        String dateTime = indexConfigDeleteRequestParam.getDateTime();
        Integer loginUserId = indexConfigDeleteRequestParam.getLoginUserId();

        Integer deleteResult = indexConfigService.deleteByIds(ids, loginUserId, dateTime);
        if (deleteResult > 0) {
            return new ResultUtil(200, "删除成功");
        } else {
            return new ResultUtil(201, "删除失败");
        }
    }


    /**
     * 查询首页各部分推荐商品的列表
     * @param configType
     * @param number
     * @return
     */
    @GetMapping("/listconfigtype/{configType}/{number}")
    @ResponseBody
    public ResultUtil getListByConfigType(@PathVariable("configType") Integer configType, @PathVariable("number") Integer number) {
        List<IndexConfig> list = indexConfigService.getListByConfigType(configType, number);
        if (Objects.nonNull(list)) {
            return new ResultUtil(200, "", list);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }
}
